<?php


namespace App\Model\Logic;


use App\Exception\HttpException;
use App\Model\Dao\ManagerDao;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Bean\Annotation\Mapping\Inject;
use Swoft\Http\Session\HttpSession;

/**
 * @Bean(name="LoginLogic")
 * 管理员登录
 * @package App\Model\Logic
 */
class LoginLogic
{
    /**
     * @Inject()
     * @var ManagerDao
     */
    private $managerDao;
    public function checkLogin($data){
        // 自定义验证
//        validate($data, 'LoginValidator', [], [UserNameValidator::class]);
        validate($data, 'LoginValidator', [], ['UserNameValidator']);
        $user = $this->managerDao->getUserFromUsername($data['username']);
        if (!$user){
            throw new HttpException('用户不存在',1);
        }
        if (!password_verify($data['password'],$user['password'])){
            throw new HttpException('密码不正确',1);
        }
        // 记录用户登录状态
        HttpSession::current()->set('userid', $user['id']);
        HttpSession::current()->set('username', $user['username']);
        throw new HttpException('登录成功',0);
    }
}
